/* MIT License
 *
 * Copyright (c) 2012 Reuben Smith, Miami University
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.droidlike.vsig;


import com.jme3.system.JmeCanvasContext;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;


/**
 * VSIGFrame.
 *
 * @author smithre5
 */
public class VSIGFrame
        extends javax.swing.JFrame
{
    /**
     * Creates new form VSIGFrame
     */
    public VSIGFrame()
    {
        initComponents();
    }


    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        simCanvas = new javax.swing.JPanel();
        simTablePane = new javax.swing.JScrollPane();
        simTable = new javax.swing.JTable();
        menuBar = new javax.swing.JMenuBar();
        menuFileMenu = new javax.swing.JMenu();
        menuFileProfileItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuFileNewSimItem = new javax.swing.JMenuItem();
        menuFileOpenSimItem = new javax.swing.JMenuItem();
        menuFileSaveSimItem = new javax.swing.JMenuItem();
        menuFileSaveSimAsItem = new javax.swing.JMenuItem();
        menuFileCloseSimItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        menuFileExitItem = new javax.swing.JMenuItem();
        menuHelpMenu = new javax.swing.JMenu();
        menuHelpAboutItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(VSIG.TITLE);
        setMinimumSize(new java.awt.Dimension(890, 528));
        setName("mainFrame"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(java.awt.event.WindowEvent evt)
            {
                formWindowClosing(evt);
            }
        });

        simCanvas.setBackground(new java.awt.Color(100, 149, 237));
        simCanvas.setBorder(null);
        simCanvas.setMinimumSize(new java.awt.Dimension(640, 480));

        javax.swing.GroupLayout simCanvasLayout = new javax.swing.GroupLayout(simCanvas);
        simCanvas.setLayout(simCanvasLayout);
        simCanvasLayout.setHorizontalGroup(
            simCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 640, Short.MAX_VALUE)
        );
        simCanvasLayout.setVerticalGroup(
            simCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        simTable.setModel(new InfoTableModel(simTable));
        simTable.setMinimumSize(new java.awt.Dimension(250, 480));
        simTablePane.setViewportView(simTable);

        menuFileMenu.setMnemonic('F');
        menuFileMenu.setText("File");

        menuFileProfileItem.setMnemonic('P');
        menuFileProfileItem.setText("Profile...");
        menuFileProfileItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                menuFileProfileItemActionPerformed(evt);
            }
        });
        menuFileMenu.add(menuFileProfileItem);
        menuFileMenu.add(jSeparator1);

        menuFileNewSimItem.setMnemonic('N');
        menuFileNewSimItem.setText("New Simulation...");
        menuFileNewSimItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                menuFileNewSimItemActionPerformed(evt);
            }
        });
        menuFileMenu.add(menuFileNewSimItem);

        menuFileOpenSimItem.setMnemonic('O');
        menuFileOpenSimItem.setText("Open Simulation...");
        menuFileOpenSimItem.setEnabled(false);
        menuFileMenu.add(menuFileOpenSimItem);

        menuFileSaveSimItem.setMnemonic('V');
        menuFileSaveSimItem.setText("Save Simulation");
        menuFileSaveSimItem.setEnabled(false);
        menuFileMenu.add(menuFileSaveSimItem);

        menuFileSaveSimAsItem.setMnemonic('S');
        menuFileSaveSimAsItem.setText("Save Simulation As...");
        menuFileSaveSimAsItem.setEnabled(false);
        menuFileMenu.add(menuFileSaveSimAsItem);

        menuFileCloseSimItem.setMnemonic('C');
        menuFileCloseSimItem.setText("Close Simulation");
        menuFileCloseSimItem.setEnabled(false);
        menuFileCloseSimItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                menuFileCloseSimItemActionPerformed(evt);
            }
        });
        menuFileMenu.add(menuFileCloseSimItem);
        menuFileMenu.add(jSeparator2);

        menuFileExitItem.setMnemonic('x');
        menuFileExitItem.setText("Exit");
        menuFileExitItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                menuFileExitItemActionPerformed(evt);
            }
        });
        menuFileMenu.add(menuFileExitItem);

        menuBar.add(menuFileMenu);

        menuHelpMenu.setMnemonic('H');
        menuHelpMenu.setText("Help");

        menuHelpAboutItem.setMnemonic('A');
        menuHelpAboutItem.setText("About");
        menuHelpAboutItem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                menuHelpAboutItemActionPerformed(evt);
            }
        });
        menuHelpMenu.add(menuHelpAboutItem);

        menuBar.add(menuHelpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(simCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(simTablePane, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(simTablePane, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
            .addComponent(simCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuFileExitItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_menuFileExitItemActionPerformed
    {//GEN-HEADEREND:event_menuFileExitItemActionPerformed
        // TODO Handle saving current state.

        stopSimulation();

        this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING,
                                           null));
    }//GEN-LAST:event_menuFileExitItemActionPerformed

    private void menuFileNewSimItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_menuFileNewSimItemActionPerformed
    {//GEN-HEADEREND:event_menuFileNewSimItemActionPerformed
        Simulation sim = VSIG.getSimulation();
        
        if (sim == null) {
            sim = VSIG.startSimulation(this);
        }
        else {
            Logger.getLogger(VSIGFrame.class.getName()).log(Level.WARNING, null, 
                "Attaching pre-existing simulation.");
        }

        attachSimulation(sim);
    }//GEN-LAST:event_menuFileNewSimItemActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing
    {//GEN-HEADEREND:event_formWindowClosing
        stopSimulation();
    }//GEN-LAST:event_formWindowClosing

    private void menuFileCloseSimItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_menuFileCloseSimItemActionPerformed
    {//GEN-HEADEREND:event_menuFileCloseSimItemActionPerformed
        stopSimulation();
    }//GEN-LAST:event_menuFileCloseSimItemActionPerformed

    private void menuHelpAboutItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuHelpAboutItemActionPerformed
        AboutDialog aboutDlg = new AboutDialog(this, true);
        aboutDlg.setLocationRelativeTo(this);
        aboutDlg.setVisible(true);
    }//GEN-LAST:event_menuHelpAboutItemActionPerformed

    private void menuFileProfileItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_menuFileProfileItemActionPerformed
    {//GEN-HEADEREND:event_menuFileProfileItemActionPerformed
        ProfileSelectDialog profileSelectDlg = new ProfileSelectDialog(this, true);
        profileSelectDlg.setLocationRelativeTo(this);
        profileSelectDlg.setVisible(true);
    }//GEN-LAST:event_menuFileProfileItemActionPerformed


    /**
     * @return the information table
     */
    public JTable getTable()
    {
        return simTable;
    }
    
    
    /**
     * Attaches the simulation's canvas to the display panel.
     * 
     * @param sim the Simulation to use
     * @return true if successful; false otherwise
     */
    private boolean attachSimulation(final Simulation sim)
    {
        if (sim == null) {
            return false;
        }

        JmeCanvasContext context = (JmeCanvasContext) sim.getContext();
        context.setSystemListener(sim);

        Dimension canvasSize = simCanvas.getSize();
        final Canvas canvas = context.getCanvas();
        canvas.setMinimumSize(canvasSize);
        canvas.setPreferredSize(canvasSize);
        canvas.setSize(canvasSize);

        simCanvas.add(context.getCanvas());

        this.addComponentListener(new ComponentListener()
        {
            public void componentResized(ComponentEvent e)
            {
                canvas.setSize(simCanvas.getSize());
                sim.getSimulationCamera().updatePerspective();
            }


            public void componentMoved(ComponentEvent e)
            {
                // STUB
            }


            public void componentShown(ComponentEvent e)
            {
                // STUB
            }


            public void componentHidden(ComponentEvent e)
            {
                // STUB
            }


        });

        menuFileProfileItem.setEnabled(false);
        menuFileNewSimItem.setEnabled(false);
        menuFileCloseSimItem.setEnabled(true);

        return true;
    }


    /**
     * Detaches and terminates the simulation. The interface is updated as
     * appropriate.
     */
    private void stopSimulation()
    {
        VSIG.stopSimulation();

        menuFileCloseSimItem.setEnabled(false);
        menuFileNewSimItem.setEnabled(true);
        menuFileProfileItem.setEnabled(true);
        
        simCanvas.removeAll();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuFileCloseSimItem;
    private javax.swing.JMenuItem menuFileExitItem;
    private javax.swing.JMenu menuFileMenu;
    private javax.swing.JMenuItem menuFileNewSimItem;
    private javax.swing.JMenuItem menuFileOpenSimItem;
    private javax.swing.JMenuItem menuFileProfileItem;
    private javax.swing.JMenuItem menuFileSaveSimAsItem;
    private javax.swing.JMenuItem menuFileSaveSimItem;
    private javax.swing.JMenuItem menuHelpAboutItem;
    private javax.swing.JMenu menuHelpMenu;
    private javax.swing.JPanel simCanvas;
    private javax.swing.JTable simTable;
    private javax.swing.JScrollPane simTablePane;
    // End of variables declaration//GEN-END:variables
}

